package com.zjsru.plan2025.oneday;

/**
 * 2269. 找到一个数字的 K 美丽值
 *
 * @Author: cookLee
 * @Date: 2025-03-10
 */
public class DivisorSubstrings {

    public static void main(String[] args) {
        DivisorSubstrings divisorSubstrings = new DivisorSubstrings();
        int num = 430043;
        int k = 2;
        System.out.println(divisorSubstrings.divisorSubstrings(num, k));
    }

    public int divisorSubstrings(int num, int k) {
        String str = String.valueOf(num);
        int len = str.length();
        int ans = 0;
        int left = 0;
        for (int i = k; i <= len; i++) {
            String substring = str.substring(left, i);
            int parseInt = Integer.parseInt(substring);
            if (parseInt == 0) {
                left++;
                continue;
            }
            if (num % parseInt == 0) {
                ans++;
            }
            left++;
        }
        return ans;
    }

    public int divisorSubstrings2(int num, int k) {
        String s = Integer.toString(num);
        int n = s.length();
        int res = 0;
        for (int i = 0; i <= n - k; ++i) {
            //枚举所有长度为 k 的子串
            int tmp = Integer.parseInt(s.substring(i, i + k));
            if (tmp != 0 && num % tmp == 0) {
                ++res;
            }
        }
        return res;
    }

}
